//
//  PCMyMessageDetailsTableViewCell.m
//  fruitsDealers
//
//  Created by sgone on 16/3/23.
//  Copyright © 2016年 com.sgone. All rights reserved.
//

#import "PCMyMessageDetailsTableViewCell.h"
#import "FDLineView.h"
#import "Common.h"
#import "labelAdaptive.h"
#import "UIView+Helper.h"
#import "DataSingleton.h"
@interface PCMyMessageDetailsTableViewCell () {
    
    
}

/**
 发信人
 */
@property (nonatomic, strong)UILabel *nameLabel;
/**
 发送时间
 */
@property (nonatomic, strong)UILabel *timeLabel;
/**
 时分秒
 */
@property (nonatomic, strong)UILabel *hourLabel;
/**
 发送内容
 */
@property (nonatomic, strong)UILabel *contentLabel;
/**
 发信人
 */
@property (nonatomic, strong)UILabel *sendLabel;
/**
 询价内容
 */
@property (nonatomic, strong)UILabel *titleLabel;
/**
 模板view
 */
@property (nonatomic, strong)UIView *templateView;
/**
 背景view
 */
@property (nonatomic, strong)UIView *backView;
/**
 下划线
 */
@property (nonatomic, strong)FDLineView *lineView;

@property (nonatomic, strong)DataSingleton *data;

@end

@implementation PCMyMessageDetailsTableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableVIew {
    
    NSString *ID = @"PCMyMessageDetailsTableViewCell";
    PCMyMessageDetailsTableViewCell *cell = [tableVIew dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle] loadNibNamed:@"PCMyMessageDetailsTableViewCell" owner:nil options:nil] lastObject];
    }
    return cell;
}


- (void)awakeFromNib {
    // Initialization code
    self.backgroundColor = [UIColor clearColor];
    self.contentView.backgroundColor = [UIColor clearColor];
    
    self.data = [DataSingleton sharedDataSingleton];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setcontrol {
    
    _backView = [[UIView alloc] init];
    _backView.backgroundColor = [UIColor whiteColor];
    _backView.layer.cornerRadius = 5;
    [self addSubview:_backView];
    
    _templateView = [[UIView alloc] init];
    _templateView.backgroundColor = [UIColor whiteColor];
    [self.backView addSubview:_templateView];
    
    _nameLabel = [[UILabel alloc] init];
    _nameLabel.font = [UIFont systemFontOfSize:14 / FDLaOt];
    _nameLabel.textColor = FDFont_COLOR;
    [self.backView addSubview:_nameLabel];
    
//    _sendLabel = [[UILabel alloc] init];
//    _sendLabel.font = [UIFont systemFontOfSize:14 / FDLaOt];
//    _sendLabel.textColor = FDFont_COLOR;
//    _sendLabel.textAlignment = NSTextAlignmentRight;
//    [self.backView addSubview:_sendLabel];
//    
    _timeLabel = [[UILabel alloc] init];
    _timeLabel.font = [UIFont systemFontOfSize:13 / FDLaOt];
    _timeLabel.textColor = FDFont_COLOR;
    _timeLabel.textAlignment = NSTextAlignmentRight;
    [self.backView addSubview:_timeLabel];
    
    _hourLabel = [[UILabel alloc] init];
    _hourLabel.font = [UIFont systemFontOfSize:12 / FDLaOt];
    _hourLabel.textColor = FDGFont_COLOR;
    _hourLabel.textAlignment = NSTextAlignmentRight;
    [self.backView addSubview:_hourLabel];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = [UIFont systemFontOfSize:13 / FDLaOt];
    _titleLabel.textColor = FDMFont_COLOR;
    _titleLabel.textAlignment = NSTextAlignmentRight;
    [self.backView addSubview:_titleLabel];
    
    _contentLabel = [[UILabel alloc] init];
    _contentLabel.font = [UIFont systemFontOfSize:13 / FDLaOt];
    _contentLabel.textColor = FDMFont_COLOR;
    _contentLabel.numberOfLines = 0;
    [self.backView addSubview:_contentLabel];
    
    _lineView = [[FDLineView alloc] init];
    [self.backView addSubview:_lineView];
}

- (CGFloat)getCellHeight {
    
    [self setcontrol];
    
//    self.sendLabel.text = @"发信人：";
    
    [checkNull(self.messageDic[@"username"]) length] > 0 ? self.nameLabel.text = self.messageDic[@"username"] : @"";
//    self.nameLabel.text = self.messageDic[@"username"];
    self.titleLabel.text = @"询价内容：";
    
    //时间戳转时间
    double lastactivityInterval = [self.messageDic[@"inputtime"] doubleValue];
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:lastactivityInterval];
    
    self.timeLabel.text = [NSString stringWithFormat:@"发送时间：%@",[formatter stringFromDate:date]];
    self.contentLabel.text = self.messageDic[@"chart_text"];
    
    [formatter setDateFormat:@"HH:mm:ss"];
    self.hourLabel.text = [formatter stringFromDate:date];
    
    CGFloat Y = 8;
    CGFloat Height = 20 / FDLaOt;
    
//    CGSize sendSize = [labelAdaptive sizeWithString:self.sendLabel.text font:self.sendLabel.font];
//    self.sendLabel.frame = CGRectMake(ViewWidth / 5 - sendSize.width, Y, sendSize.width, Height);
    
    
    
    

    
    
    if ([self.messageDic[@"uid"] intValue] != [[self.data getLocalData:@"wId"] intValue])
    {
        
        self.nameLabel.textColor = BenSe_COLOR;
   
    }
    else
    {
    
        self.nameLabel.textColor = FDFont_COLOR;
    
    }
    CGSize nameSize = [labelAdaptive sizeWithString:self.nameLabel.text font:self.nameLabel.font];
    self.nameLabel.frame = CGRectMake(2, self.nameLabel.y, nameSize.width, Height);
    
    CGSize timeSize = [labelAdaptive sizeWithString:self.timeLabel.text font:self.timeLabel.font];
    self.timeLabel.frame = CGRectMake(ViewWidth - 8 * 2 - timeSize.width - 4, self.nameLabel.y, timeSize.width, Height);
    
    CGSize replySize = [labelAdaptive sizeWithString:self.hourLabel.text font:self.hourLabel.font];
    self.hourLabel.frame = CGRectMake(ViewWidth / 5 - replySize.width - 11 / FDLaOt, self.nameLabel.y + self.nameLabel.height + 4, replySize.width, Height);
    
    CGSize titleSize = [labelAdaptive sizeWithString:self.titleLabel.text font:self.titleLabel.font];
    self.titleLabel.frame = CGRectMake(ViewWidth / 5 - titleSize.width+4, self.hourLabel.y + self.hourLabel.height + Y / 2, titleSize.width, titleSize.height);
    
    CGSize contentSize = [labelAdaptive sizeWithString:self.contentLabel.text font:self.contentLabel.font];
        
    self.contentLabel.frame = CGRectMake(ViewWidth / 5, self.hourLabel.y + self.hourLabel.height + Y / 2, ViewWidth - 8 * 2 - ViewWidth / 5 - 8, contentSize.height);
    
    self.backView.frame = CGRectMake(8, 0, ViewWidth - 8 * 2, self.contentLabel.y + self.contentLabel.height + 8);
    
    if (self.row == 0) {
        
        self.templateView.frame = CGRectMake(0, self.backView.height - 5, self.backView.width, 5);
    } else if (self.row == self.array.count - 1) {
        
        self.templateView.frame = CGRectMake(0, 0, self.backView.width, 5);
    } else {
        
        self.templateView.frame = CGRectMake(0, 0, self.backView.width, self.backView.height);
    }
    
    if (self.row != self.array.count - 1) {
        
        self.lineView.frame = CGRectMake(8, self.backView.height - 0.5, self.backView.width - 8 * 2, 0.5);
    }
    
    return self.backView.height;
}

@end
